#include <bits/stdc++.h>
using namespace std;
#define int long long
int t, n, q, k, pos[100005], a[200005], cnt[200005];
bitset<200001> can[101], vis;

void solve()
{
    vector<pair<int, int>> f, g;
    for (int i = 1; i <= n; i++)
        for (int j = pos[i], lst = -1e9; j < pos[i + 1]; j++) {
            if (j - lst < k)
                f.emplace_back(i, j), can[1][a[j]] = true;
            if (a[j] == 1)
                lst = j;
        }
    for (int i = 2; i <= 100; i++) {
        memset(cnt, 0xff, sizeof cnt);
        for (auto x : f) {
            if (cnt[a[x.second]] == -1)
                cnt[a[x.second]] = x.first;
            else if (cnt[a[x.second]] != x.first)
                cnt[a[x.second]] = 0;
        }
        f.clear();
        for (int j = 1; j <= n; j++)
            for (int p_idx = pos[j]; p_idx < pos[j + 1]; p_idx++)
                if (~cnt[a[p_idx]] && cnt[a[p_idx]] != j)
                    g.emplace_back(j, p_idx);
        for (auto x : g)
            vis[x.second] = true;
        for (int j = 1; j <= n; j++)
            for (int p_idx = pos[j], lst = -1e9; p_idx < pos[j + 1]; p_idx++) {
                if (p_idx - lst < k)
                    f.emplace_back(j, p_idx), can[i][a[p_idx]] = true;
                if (vis[p_idx])
                    lst = p_idx;
            }
        for (auto x : g)
            vis[x.second] = false;
        g.clear();
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> t;
    while (t--) {
        for (int i = 1; i <= 101; i++)
            can[i].reset();
        vis.reset();
        cin >> n >> k >> q;
        for (int i = 1; i <= n; i++) {
            int x;
            cin >> x;
            pos[i + 1] = pos[i] + x;
            for (int j = pos[i]; j < pos[i + 1]; j++)
                cin >> a[j];
        }
        solve();
        for (int i = 1; i <= q; i++) {
            int x, y;
            cin >> x >> y;
            cout << can[x][y] << "\n";
        }
    }
    return 0;
}